package com.hot12345.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hot12345.entity.Info;
import com.hot12345.server.DBConnection.IbatisConnectCommImpl;
import com.hot12345.vo.InfoVO;
import com.hot12345.database.BaseDaoImpl;
import com.ibatis.sqlmap.client.SqlMapClient;

public class InfoDaoImpl extends BaseDaoImpl<Info> {

	private final static Log log = LogFactory.getLog(InfoDaoImpl.class);
	private final static InfoDaoImpl instance = new InfoDaoImpl();
	public final static String RDS = "defaultRDs";
	public final static String WDS = "defaultWDs";
	
	private InfoDaoImpl(){}
	
	public static InfoDaoImpl getInstance(){
		return instance;
	}
	
	public List<InfoVO> findByIds(Map<String,Object> params) throws Exception{
		if(params==null){
			params=new HashMap<String, Object>();
		}
		String findSqlMapId = getEntityName() + ".findByIds";
		log.debug("findSqlMapId=" + findSqlMapId);
//		logger.warn("数据源"+RDS);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(RDS);
		try {
			return (List<InfoVO>) sqlClient.queryForList(
					findSqlMapId, params);
		} catch (SQLException e) {
			log.error("SQLException ",e);
			 throw new SQLException("jdbc.error.findByParams"+e);
		}
	}
	
	/**
	 * 置顶操作
	 * 	
	 * @param id
	 * @return
	 * @author wuzongbao
	 * @date 2013-5-15 下午09:43:49
	 * @version V1.0
	 * @throws Exception 
	 */
	public int setIsTop(long id) throws Exception{
		Info info = new Info();
		info.setId(id);
		info.setIsTop(1);
		info.setTopTime(System.currentTimeMillis());
		InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
		return infoDao.update(info);
	}
	
	/**
	 * 取消置顶
	 *	
	 * @param id
	 * @return
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-5-15 下午10:23:23
	 * @version V1.0
	 */
	public int cancelTop(long id) throws Exception{
		
		String updateSqlMapId = getEntityName() + ".cancelTop";
		log.debug("updateSqlMapId=" + updateSqlMapId);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(WDS);
		int effectCnt = 0;
		try {
			effectCnt = sqlClient.update(updateSqlMapId, id);
			if(effectCnt==0){
				log.error("update failure");
				throw new Exception("jdbc.error.update");  
			}
		} catch (SQLException e) {
			log.error("SQLException " ,e);
			throw new Exception("jdbc.error.update");  
		}
		return effectCnt;
	}
}
